package com.persondemo.videoappliction.ui.duanzi;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.widget.ImageView;
import android.widget.TextView;

import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.ui.base.AppManager;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.utils.X5WebView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class XingWenWebActivity extends BaseActivity {
    @BindView(R.id.webview)
    X5WebView webview;
    String url = "", title = "";
    @BindView(R.id.xingwen_web_back)
    ImageView xingwenWebBack;
    @BindView(R.id.title_xingwen)
    TextView titleXingwen;

    @Override
    public void onRetry() {

    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_xingwen_webiew;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {

    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {

    }

    @Override
    public void initData() {
        webview.setDrawingCacheEnabled(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 优先使用缓存
        webview.getSettings().setAppCacheMaxSize(50 * 1024 * 1024);// 缓存最多可以有50M
        Intent intent = getIntent();
        if (intent != null) {
            url = intent.getStringExtra("title");
            title = intent.getStringExtra("text");
            Log.i("guanggao", url);
            webview.loadUrl(url);
        }
        if (!TextUtils.isEmpty(title)) {
            titleXingwen.setText(title);
        }
        String userAgent = webview.getSettings().getUserAgentString();
        if (!TextUtils.isEmpty(userAgent)) {
            webview.getSettings().setUserAgentString(userAgent
                    .replace("Android", "")
                    .replace("android", "")
                    + " cldc");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                ArrayList<View> outView = new ArrayList<View>();
                getWindow().getDecorView().findViewsWithText(outView, "QQ浏览器", View.FIND_VIEWS_WITH_TEXT);
                int size = outView.size();
                if (outView != null && outView.size() > 0) {
                    outView.get(0).setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webview != null) {
            webview.destroy();
            webview = null;
        }
    }

    @OnClick(R.id.xingwen_web_back)
    public void onViewClicked() {
        AppManager.getAppManager().finishActivity();
    }
}
